
#import "GameManager.h"

float W=320;
float H=480;

@implementation GameManager

#pragma mark ============================= Init Methods ================================

+ (GameManager*) getInstance  {
    
    static GameManager* gameManager;
		
	if (!gameManager) {
        gameManager = [[GameManager alloc] init];
		[gameManager preloader];            
	}
	    
    return gameManager;
}

- (void) preloader {   
    //Preload OGL-Textures          
    [self getTex: @"asteroid.png" isImage: YES];   
    [self getTex: @"obstacle.png" isImage: YES];
    
    //Optional 
    //[self getTex: @"abc" isImage: NO]; 
    
    [self setOGLProjection];
    
    state = LOAD_GAME;
}

- (void) loadGame { 
    //Optional
    //[self removeFromDictionary: @"myTex1.png"];          
}

#pragma mark ============================= Game Handler ===============================

- (void) touchBegan: (CGPoint) p {      
    NSLog(@"Touch: %f %f", p.x, p.y);
}

- (void) touchMoved: (CGPoint) p {
    [self touchBegan: p];      
}

- (void) touchEnded {
}

- (void) drawStatesWithFrame: (CGRect) frame { 
    W = frame.size.width;
    H = frame.size.height; 
    switch (state) {
        case LOAD_GAME: 
            [self loadGame];            
            state = PLAY_GAME;
            break;  
        case PLAY_GAME:
            [self playGame];
            break;           
        default: NSLog(@"ERROR: Unknown state: %i", state);
            break;
    }    
}	 

- (void) playGame {
    timer++;     
 
    static float z = 0; z -= 0.5;
    [self setCameraX: 0 cameraY: 0 cameraZ: z
             lookAtX: 0 lookAtY: 0 lookAtZ: z-1]; 
    
    [self drawAsteroid];
    [self drawObstacle];   
} 


#pragma mark ============================= OGL Methods ===============================

- (void) setOGLProjection {    
    glLoadIdentity();
    
    //Set View
    glMatrixMode(GL_PROJECTION);     
    zNear = 0.1; 
    zFar = 2500;    
    fieldOfViewAngle = 45;
    float top = zNear * tan(M_PI * fieldOfViewAngle / W);
    float bottom = -top;
    float left = bottom * W / H; 
    float right = top * W / H;    
    glFrustumf(left, right, bottom, top, zNear, zFar); 

    glMatrixMode(GL_MODELVIEW);
    glEnableClientState(GL_VERTEX_ARRAY);    
    glEnable(GL_DEPTH_TEST); 
    glDepthFunc(GL_LESS);
    
    [self setLight];
} 

- (void) setLight {    
    glEnable(GL_LIGHTING);      
    glEnable(GL_COLOR_MATERIAL); 
    
    glEnable(GL_LIGHT0);
    GLfloat color0[ ] = {1, 1, 1, 1};   
    glLightfv(GL_LIGHT0, GL_DIFFUSE, color0);     
    GLfloat light_position0[ ] = {0, 1, 0, 0};  
    glLightfv(GL_LIGHT0, GL_POSITION, light_position0); 
}

- (void) drawAsteroid {
    Tex *tex = [[GameManager getInstance] getTex: @"asteroid.png" isImage: YES];
    GLuint textureID = [tex getTextureID]; 
    
    GLfloat vertices[ ] = { 
        -0.207126, -0.066316, 17.863581, 23.264721, -1.502157, -0.192209, -0.207126, 21.969690, 0.095400, -23.678974, -2.669385, 0.198593, -0.027670, -21.068602, -2.464037, -3.388360, -1.154920, -20.450109, 11.528797, -0.568606, 16.393007, 20.120089, -1.276288, 10.694796, -0.809213, 9.257827, 17.140844, -0.207126, 18.825060, 10.966887, -11.943050, -0.595412, 16.425299, -20.534342, -1.295913, 10.719969, -0.027670, -10.287013, 15.000261, -0.027670, -14.472719, 6.682102, 7.230282, -1.594619, -17.014429, 20.120089, -1.502157, -11.752726, -0.207126, 10.251815, -18.096594, -0.207126, 18.825060, -10.404342, -11.943050, -0.948562, -18.193747, -20.534342, -1.023628, -10.605993, -0.207126, -12.332569, -18.981672, -0.207126, -19.508356, -10.751425, 20.120089, 10.233767, -0.180500, 11.528797, 18.825060, -0.016802, -11.943050, 18.825060, -0.016802, -17.136053, 8.313806, -0.397686, -20.534342, -12.443668, 0.692919, -11.763594, -18.917589, 0.491507, 11.708252, -19.362967, -0.575785, 20.120089, -12.799344, -0.269648, 11.010029, 10.023398, 8.090002, -13.063187, 11.399381, 13.485727, -12.883731, -12.294924, 12.127554, 12.828390, -12.320704, 12.112505, 12.648933, 11.353903, -13.732217, -13.063187, 11.416142, -13.583072, -13.063187, -12.948364, -12.757673, 12.648933, -13.815801, -14.891082, -0.027670, -17.775347, 0.462729, -0.027670, -10.785804, 11.093355, -7.974440, -15.447367, 2.195024, 7.919100, -15.781916, 1.398743, -8.678226, -11.502975, 8.904417, 8.622885, -11.594921, 8.715879, 0.203016, -1.274000, -17.947115, 12.775757, -1.492089, -13.838882, 2.339248, 5.321407, -15.823799, 2.339248, -7.455033, -16.421265, 8.432550, 5.751537, -14.457232, 8.432550, -8.197471, -15.256260, -19.971733, 2.595546, -0.710552, -17.895210, 2.839720, 6.113892, -18.807550, 3.640034, -6.871016, -14.279131, 14.036302, -0.327436, -14.053627, 9.062331, 7.468456, -14.965967, 9.768659, -8.346003, 10.600231, 2.221758, 12.016981, 15.251944, 1.856801, 9.047716, 2.803887, 9.638645, 12.474089, 2.803887, 13.992887, 9.423529, 15.251944, 9.305200, 2.728025, 10.600231, 13.956913, 2.816954, -0.207126, 21.969690, 0.095400, 11.528797, 18.825060, -0.016802, 11.528797, 18.825060, -0.016802, 11.528797, 18.825060, -0.016802, 20.120089, 10.233767, -0.180500, 20.120089, 10.233767, -0.180500, 20.120089, 10.233767, -0.180500, 23.264721, -1.502157, -0.192209, -19.971733, 2.595546, -0.710552, -17.136053, 8.313806, -0.397686, -17.136053, 8.313806, -0.397686, -17.136053, 8.313806, -0.397686, -14.279131, 14.036302, -0.327436, -11.943050, 18.825060, -0.016802, -11.943050, 18.825060, -0.016802, -0.207126, 21.969690, 0.095400, -11.763594, -18.917589, 0.491507, -11.763594, -18.917589, 0.491507, -11.763594, -18.917589, 0.491507, -20.534342, -12.443668, 0.692919, -20.534342, -12.443668, 0.692919, -20.534342, -12.443668, 0.692919, -23.678974, -2.669385, 0.198593, 23.264721, -1.502157, -0.192209, 20.120089, -12.799344, -0.269648, 20.120089, -12.799344, -0.269648, 20.120089, -12.799344, -0.269648, 11.708252, -19.362967, -0.575785, 11.708252, -19.362967, -0.575785, 11.708252, -19.362967, -0.575785, -0.027670, -21.068602, -2.464037, -0.027670, -21.068602, -2.464037, -23.678974, -2.669385, 0.198593, -19.971733, 2.595546, -0.710552, -19.971733, 2.595546, -0.710552, -14.279131, 14.036302, -0.327436, -14.279131, 14.036302, -0.327436, -11.943050, 18.825060, -0.016802
    };    
    
    GLfloat textureCoords[ ] = { 
        0.511933, 0.499667, 0.000000, 0.753190, 0.915463, 0.000000, 0.054368, 0.673368, 0.000000, 0.376911, 0.032117, 0.000000, 0.951099, 0.414459, 0.000000, 0.484371, 0.416213, 0.000000, 0.596259, 0.660468, 0.000000, 0.674727, 0.798045, 0.000000, 0.365551, 0.557660, 0.000000, 0.221873, 0.618785, 0.000000, 0.443365, 0.331518, 0.000000, 0.400423, 0.184292, 0.000000, 0.669252, 0.429368, 0.000000, 0.765638, 0.383536, 0.000000, 0.503857, 0.572961, 0.000000, 0.521286, 0.787911, 0.000000, 0.294277, 0.472547, 0.000000, 0.167899, 0.471408, 0.000000, 0.471063, 0.289744, 0.000000, 0.444918, 0.159709, 0.000000, 0.668695, 0.449126, 0.000000, 0.821048, 0.415973, 0.000000, 0.523282, 0.987138, 0.000000, 0.277151, 0.915329, 0.000000, 0.007749, 0.456259, 0.000000, 0.138496, 0.176471, 0.000000, 0.560975, 0.017201, 0.000000, 0.781952, 0.165165, 0.000000, 0.945935, 0.519981, 0.000000, 0.910399, 0.745701, 0.000000, 0.418539, 0.775995, 0.000000, 0.206724, 0.393945, 0.000000, 0.615961, 0.234995, 0.000000, 0.784290, 0.594949, 0.000000, 0.300945, 0.669701, 0.000000, 0.250696, 0.287367, 0.000000, 0.649960, 0.224705, 0.000000, 0.729684, 0.635959, 0.000000, 0.843384, 0.348170, 0.000000, 0.709311, 0.409802, 0.000000, 0.753197, 0.247620, 0.000000, 0.874056, 0.478244, 0.000000, 0.663767, 0.283529, 0.000000, 0.792351, 0.519477, 0.000000, 0.490483, 0.469265, 0.000000, 0.508666, 0.655244, 0.000000, 0.386401, 0.509299, 0.000000, 0.592850, 0.493051, 0.000000, 0.386555, 0.600114, 0.000000, 0.611951, 0.582375, 0.000000, 0.248617, 0.090600, 0.000000, 0.278686, 0.201907, 0.000000, 0.341885, 0.134101, 0.000000, 0.067374, 0.326368, 0.000000, 0.185671, 0.305024, 0.000000, 0.244802, 0.205051, 0.000000, 0.541431, 0.703632, 0.000000, 0.581166, 0.799010, 0.000000, 0.370820, 0.658315, 0.000000, 0.304298, 0.689334, 0.000000, 0.462506, 0.876245, 0.000000, 0.344768, 0.858742, 0.000000, 0.049151, 0.482785, 0.000000, 0.111095, 0.697170, 0.000000, 0.111095, 0.697170, 0.000000, 0.111095, 0.697170, 0.000000, 0.264228, 0.932092, 0.000000, 0.264228, 0.932092, 0.000000, 0.264228, 0.932092, 0.000000, 0.543030, 0.948962, 0.000000, 0.342132, 0.062312, 0.000000, 0.233245, 0.116173, 0.000000, 0.233245, 0.116173, 0.000000, 0.233245, 0.116173, 0.000000, 0.131924, 0.203011, 0.000000, 0.076760, 0.286514, 0.000000, 0.076760, 0.286514, 0.000000, 0.049151, 0.482785, 0.000000, 0.864629, 0.131448, 0.000000, 0.864629, 0.131448, 0.000000, 0.864629, 0.131448, 0.000000, 0.706831, 0.027238, 0.000000, 0.706831, 0.027238, 0.000000, 0.706831, 0.027238, 0.000000, 0.468322, -0.010942, 0.000000, 0.543030, 0.948962, 0.000000, 0.816401, 0.859411, 0.000000, 0.816401, 0.859411, 0.000000, 0.816401, 0.859411, 0.000000, 0.962139, 0.651005, 0.000000, 0.962139, 0.651005, 0.000000, 0.962139, 0.651005, 0.000000, 0.936496, 0.297611, 0.000000, 0.936496, 0.297611, 0.000000, 0.468322, -0.010942, 0.000000, 0.342132, 0.062312, 0.000000, 0.342132, 0.062312, 0.000000, 0.131924, 0.203011, 0.000000, 0.131924, 0.203011, 0.000000, 0.076760, 0.286514, 0.000000
    };
    
    GLfloat normals[ ] = {
        0.003697, -0.091516, 0.995797, 0.960635, -0.009128, 0.277662, 0.000000, 0.960620, 0.277864, -0.952034, 0.153941, 0.264449, -0.027300, -0.982476, -0.184377, 0.100544, 0.032613, -0.994398, 0.431710, 0.228092, 0.872697, 0.751111, 0.159021, 0.640737, 0.182587, 0.391355, 0.901944, 0.184230, 0.749000, 0.636442, -0.345698, -0.066369, 0.935996, -0.848861, 0.075828, 0.523149, -0.009580, -0.713061, 0.701036, 0.003371, -0.839616, 0.543170, 0.303673, 0.030996, -0.952272, 0.763386, 0.008987, -0.645880, 0.094384, 0.352707, -0.930961, -0.000691, 0.853692, -0.520777, -0.469468, -0.007221, -0.882920, -0.876170, 0.048828, -0.479522, 0.043528, -0.377479, -0.924995, -0.051291, -0.900582, -0.431650, 0.543550, 0.483431, 0.686184, 0.498526, 0.536516, 0.680898, -0.593064, 0.747824, 0.298386, -0.857456, 0.432127, 0.279348, -0.737814, -0.535100, 0.411459, -0.038091, -0.775229, 0.630531, 0.074511, -0.775869, 0.626478, 0.751808, -0.536277, 0.383656, 0.549312, 0.545938, 0.632619, -0.608618, 0.503179, 0.613510, -0.267024, -0.698975, 0.663425, 0.290151, -0.697242, 0.655490, 0.501162, 0.458294, -0.734032, -0.584840, 0.546924, -0.599029, -0.546715, -0.590989, -0.593157, 0.482835, -0.480230, -0.732291, 0.019595, -0.744662, 0.667155, -0.022298, -0.938299, 0.345106, 0.139119, -0.747562, 0.649459, -0.115962, -0.745121, 0.656771, 0.200090, -0.917767, 0.343028, -0.182966, -0.931377, 0.314738, 0.436004, 0.037244, -0.899174, 0.337312, 0.028683, -0.940956, 0.388036, -0.005606, -0.921627, 0.366542, 0.064780, -0.928144, 0.265028, 0.029359, -0.963794, 0.283971, 0.046665, -0.957697, -0.822885, 0.528548, 0.208561, -0.820617, 0.554704, 0.137446, -0.878331, 0.457948, -0.137182, -0.897719, 0.391166, 0.202708, -0.920951, 0.375132, 0.105472, -0.863365, 0.479034, -0.158518, 0.503376, 0.643561, 0.576579, 0.400064, 0.658891, 0.637033, 0.596823, 0.482096, 0.641394, 0.644496, 0.375890, 0.665832, 0.491379, 0.530631, 0.690636, 0.555045, 0.470129, 0.686225, 0.246129, 0.928489, -0.278079, 0.246129, 0.928489, -0.278079, 0.372092, 0.827537, -0.420393, 0.668623, 0.674584, -0.312858, 0.668623, 0.674584, -0.312858, 0.813113, 0.408711, -0.414492, 0.934158, 0.250560, -0.254105, 0.934158, 0.250560, -0.254105, -0.890027, 0.446425, -0.092504, -0.890027, 0.446425, -0.092504, -0.853748, 0.501128, -0.141368, -0.882397, 0.442494, -0.159922, -0.882397, 0.442494, -0.159922, -0.372558, 0.827060, -0.420919, -0.246129, 0.928489, -0.278079, -0.246129, 0.928489, -0.278079, -0.220149, -0.959481, -0.175872, -0.382416, -0.855909, -0.348107, -0.572756, -0.766956, -0.289360, -0.572756, -0.766956, -0.289360, -0.798693, -0.423219, -0.427756, -0.900793, -0.305419, -0.308694, -0.900793, -0.305419, -0.308694, 0.932327, -0.257780, -0.253607, 0.932327, -0.257780, -0.253607, 0.826731, -0.401049, -0.394557, 0.600691, -0.757978, -0.254243, 0.600691, -0.757978, -0.254243, 0.291806, -0.898164, -0.328863, 0.170540, -0.967661, -0.185872, 0.066341, -0.662828, 0.745827, -0.023714, -0.664104, 0.747264, -0.820168, 0.550779, -0.154812, -0.820168, 0.550779, -0.154812, -0.904151, 0.415232, -0.100462, -0.870470, 0.461535, -0.171077, -0.881642, 0.440988, -0.168041, -0.881642, 0.440988, -0.168041
    };
    
    GLushort faces[ ] = { 
        0, 6, 8, 56, 57, 30, 56, 30, 58, 58, 30, 59, 7, 1, 22, 57, 60, 30, 30, 60, 61, 30, 61, 59, 9, 23, 2, 0, 8, 10, 8, 9, 31, 8, 31, 10, 10, 31, 11, 9, 2, 24, 9, 24, 31, 54, 53, 25, 54, 25, 51, 51, 25, 50, 0, 10, 12, 10, 11, 32, 10, 32, 12, 39, 42, 13, 11, 3, 26, 11, 26, 32, 32, 26, 27, 42, 40, 13, 13, 40, 38, 0, 12, 6, 39, 13, 43, 12, 33, 6, 6, 33, 7, 13, 38, 41, 13, 41, 43, 33, 28, 29, 33, 29, 7, 7, 29, 1, 44, 46, 14, 16, 17, 34, 46, 48, 14, 14, 48, 45, 17, 62, 63, 17, 64, 34, 34, 65, 66, 34, 67, 15, 15, 68, 69, 5, 18, 16, 18, 19, 35, 18, 35, 16, 16, 35, 17, 52, 70, 71, 52, 72, 55, 55, 73, 74, 35, 75, 17, 17, 76, 77, 5, 20, 18, 20, 21, 36, 20, 36, 18, 18, 36, 19, 21, 4, 78, 21, 79, 36, 36, 80, 81, 36, 82, 19, 19, 83, 84, 44, 14, 47, 14, 45, 49, 14, 49, 47, 20, 37, 21, 15, 85, 86, 15, 87, 37, 37, 88, 89, 37, 90, 21, 21, 91, 4, 12, 32, 42, 42, 39, 12, 32, 27, 40, 40, 42, 32, 27, 92, 38, 38, 40, 27, 33, 12, 39, 39, 43, 33, 38, 28, 41, 28, 38, 93, 28, 33, 43, 43, 41, 28, 44, 5, 16, 16, 46, 44, 16, 34, 48, 48, 46, 16, 34, 15, 45, 45, 48, 34, 44, 20, 5, 20, 44, 47, 15, 37, 49, 49, 45, 15, 49, 20, 47, 20, 49, 37, 51, 31, 54, 31, 51, 11, 31, 24, 53, 53, 54, 31, 50, 11, 51, 11, 50, 3, 35, 19, 52, 52, 55, 35, 19, 94, 95, 96, 52, 19, 97, 35, 55, 35, 98, 99, 8, 6, 56, 56, 58, 8, 6, 7, 57, 57, 56, 6, 9, 8, 58, 58, 59, 9, 57, 22, 60, 22, 57, 7, 22, 23, 61, 61, 60, 22, 61, 9, 59, 9, 61, 23
    };
    
    //int verticesLength = sizeof(vertices) / sizeof(GLfloat);
    int facesLength = sizeof(faces) / sizeof(GLushort);
        
    glEnable(GL_TEXTURE_2D); 
    glEnableClientState(GL_NORMAL_ARRAY);          
    glEnable(GL_NORMALIZE);     
    
    glEnable(GL_CULL_FACE); 
    glCullFace(GL_BACK);    
    
    glVertexPointer(3, GL_FLOAT, 0, vertices);
    glBindTexture(GL_TEXTURE_2D, textureID);     
    glTexCoordPointer(3, GL_FLOAT, 0, textureCoords);        
    glNormalPointer(GL_FLOAT, 0, normals);
    
    glColor4f(1, 1, 1, 1); 
    
    glPushMatrix();  
    glTranslatef(0, 0, -200);
    static int angle = 0; angle += 2;
    glRotatef(angle, 1, 1, 0);    
    //glDrawArrays(GL_TRIANGLE_STRIP, 0, verticesLength/3); 
    glDrawElements(GL_TRIANGLES, facesLength, GL_UNSIGNED_SHORT, faces);        
    glPopMatrix();
    
    //Cleanup
    glDisable(GL_TEXTURE_2D);   
    glDisable(GL_NORMALIZE);     
    glDisable(GL_CULL_FACE);    
}

- (void) drawObstacle {
    Tex *tex = [[GameManager getInstance] getTex: @"obstacle.png" isImage: YES];
    GLuint textureID = [tex getTextureID]; 
    
    GLfloat vertices[ ] = { 
        17.474174, -0.120721, -9.998633, 14.994154, 3.820796, -8.566791, 10.981397, 2.315269, -6.250025, 10.981397, -2.556715, -6.250027, 14.994154, -4.062240, -8.566794, 17.474174, -0.120725, 10.178808, 14.994154, 3.820793, 8.746969, 10.981397, 2.315268, 6.430201, 10.981397, -2.556717, 6.430199, 14.994154, -4.062243, 8.746965, -17.474178, -0.120725, 10.178808, -14.994157, 3.820793, 8.746969, -10.981400, 2.315268, 6.430202, -10.981400, -2.556717, 6.430201, -14.994157, -4.062243, 8.746965, -17.474178, -0.120721, -9.998632, -14.994157, 3.820796, -8.566792, -10.981400, 2.315269, -6.250025, -10.981400, -2.556715, -6.250027, -14.994157, -4.062240, -8.566792, 3.941536, -4.062239, -13.683037, 2.435998, -2.556717, -9.918725, -2.435991, -2.556709, -11.183721, -0.000002, -0.120719, -11.325150, -3.941512, -4.062238, -14.948039, 3.941538, -4.062244, 13.539957, 2.435998, -2.556722, 9.775650, -0.000002, -0.120726, 11.182074, -2.435991, -2.556715, 9.775656, -3.941512, -4.062244, 15.128210, 3.941536, 3.820774, 15.128210, 2.435998, 2.315267, 9.775652, -2.435989, 2.315266, 9.775652, -3.941512, 3.820807, 13.539961, 3.941539, 3.820780, -14.948035, 2.435998, 2.315271, -9.918728, -2.435989, 2.315270, -9.918731, -3.941512, 3.820808, -13.683040, 0.000002, -0.120718, -18.822365, 0.000007, -17.594896, -9.998640, 3.941525, -15.114878, -8.566797, 2.435999, -11.102120, -6.250030, -2.435986, -11.102120, -6.250030, -3.941512, -15.114878, -8.566797, 0.000007, -17.594904, 10.178804, 3.941525, -15.114881, 8.746962, 2.435999, -11.102124, 6.430199, -2.435986, -11.102124, 6.430199, -3.941512, -15.114881, 8.746962, 0.000002, -0.120727, 18.679285, 0.000007, 17.353449, 10.178814, 3.941525, 14.873430, 8.746973, 2.435999, 10.860673, 6.430202, -2.435986, 10.860673, 6.430202, -3.941512, 14.873430, 8.746973, 0.000007, 17.353456, -9.998627, 3.941525, 14.873433, -8.566788, 2.435999, 10.860677, -6.250025, -2.435986, 10.860677, -6.250025, -3.941512, 14.873433, -8.566788, 14.994154, 3.820796, -8.566791, 3.941539, 3.820780, -14.948035, 3.941539, 3.820780, -14.948035, 10.981397, 2.315269, -6.250025, 10.981397, 2.315269, -6.250025, 2.435998, 2.315271, -9.918728, 17.474174, -0.120721, -9.998633, 14.994154, 3.820796, -8.566791, 17.474174, -0.120721, -9.998633, 14.994154, 3.820796, -8.566791, 10.981397, 2.315269, -6.250025, 14.994154, 3.820793, 8.746969, 14.994154, 3.820796, -8.566791, 10.981397, 2.315269, -6.250025, 10.981397, -2.556715, -6.250027, 10.981397, 2.315268, 6.430201, 10.981397, 2.315269, -6.250025, 10.981397, -2.556715, -6.250027, 14.994154, -4.062240, -8.566794, 10.981397, -2.556717, 6.430199, 10.981397, -2.556715, -6.250027, 14.994154, -4.062240, -8.566794, 17.474174, -0.120721, -9.998633, 14.994154, -4.062240, -8.566794, 17.474174, -0.120725, 10.178808, 14.994154, 3.820793, 8.746969, 17.474174, -0.120725, 10.178808, 14.994154, -4.062243, 8.746965, 14.994154, -4.062243, 8.746965, 10.981397, -2.556717, 6.430199, 14.994154, -4.062243, 8.746965, 17.474174, -0.120725, 10.178808, 14.994154, -4.062243, 8.746965, 0.000002, -0.120727, 18.679285, 0.000002, -0.120727, 18.679285, -2.435991, -2.556715, 9.775656, -10.981400, -2.556717, 6.430201, -2.435991, -2.556715, 9.775656, 0.000002, -0.120727, 18.679285, -17.474178, -0.120725, 10.178808, -14.994157, 3.820793, 8.746969, -17.474178, -0.120725, 10.178808, -14.994157, 3.820793, 8.746969, -10.981400, 2.315268, 6.430202, -14.994157, 3.820793, 8.746969, -10.981400, 2.315268, 6.430202, -10.981400, -2.556717, 6.430201, -10.981400, 2.315268, 6.430202, -10.981400, -2.556717, 6.430201, -14.994157, -4.062243, 8.746965, -10.981400, -2.556715, -6.250027, -10.981400, -2.556717, 6.430201, -14.994157, -4.062243, 8.746965, -17.474178, -0.120725, 10.178808, -14.994157, -4.062243, 8.746965, 0.000002, -0.120718, -18.822365, -17.474178, -0.120721, -9.998632, -14.994157, 3.820796, -8.566792, -14.994157, 3.820796, -8.566792, 0.000002, -0.120718, -18.822365, -14.994157, 3.820796, -8.566792, -10.981400, 2.315269, -6.250025, -14.994157, 3.820796, -8.566792, -10.981400, 2.315269, -6.250025, -10.981400, -2.556715, -6.250027, -10.981400, 2.315269, -6.250025, -0.000002, -0.120719, -11.325150, -17.474178, -0.120721, -9.998632, 0.000002, -0.120718, -18.822365, -14.994157, -4.062240, -8.566792, -17.474178, -0.120721, -9.998632, -10.981400, -2.556715, -6.250027, -14.994157, -4.062240, -8.566792, -3.941512, -4.062238, -14.948039, -3.941512, -4.062238, -14.948039, -2.435991, -2.556709, -11.183721, -10.981400, -2.556715, -6.250027, 14.994154, 3.820793, 8.746969, 10.981397, 2.315268, 6.430201, 14.994154, 3.820793, 8.746969, -0.000002, -0.120726, 11.182074, 10.981397, 2.315268, 6.430201, 10.981397, -2.556717, 6.430199, -0.000002, -0.120726, 11.182074, 10.981397, -2.556717, 6.430199, 2.435998, 2.315267, 9.775652, 10.981397, 2.315268, 6.430201, -0.000002, -0.120726, 11.182074, 0.000002, -0.120718, -18.822365, 3.941536, -4.062239, -13.683037, 0.000002, -0.120718, -18.822365, 2.435998, -2.556717, -9.918725, 3.941536, -4.062239, -13.683037, 2.435998, -2.556717, -9.918725, 2.435998, -2.556717, -9.918725, -2.435991, -2.556709, -11.183721, -2.435991, -2.556709, -11.183721, 2.435998, -2.556717, -9.918725, -0.000002, -0.120719, -11.325150, -2.435991, -2.556709, -11.183721, -3.941512, -4.062238, -14.948039, -2.435986, -11.102120, -6.250030, -2.435986, -11.102120, -6.250030, -2.435991, -2.556709, -11.183721, -3.941512, -4.062238, -14.948039, 0.000002, -0.120718, -18.822365, -3.941512, -4.062238, -14.948039, -3.941512, -15.114878, -8.566797, -3.941512, -4.062238, -14.948039, 0.000007, -17.594896, -9.998640, 3.941525, -15.114878, -8.566797, 0.000007, -17.594896, -9.998640, 3.941525, -15.114878, -8.566797, 2.435999, -11.102120, -6.250030, 3.941525, -15.114878, -8.566797, 2.435999, -11.102120, -6.250030, -2.435986, -11.102120, -6.250030, 2.435999, -11.102124, 6.430199, 2.435999, -11.102120, -6.250030, -2.435986, -11.102120, -6.250030, -3.941512, -15.114878, -8.566797, -2.435986, -11.102120, -6.250030, -3.941512, -15.114878, -8.566797, 0.000007, -17.594896, -9.998640, -3.941512, -15.114878, -8.566797, 0.000002, -0.120727, 18.679285, 3.941525, -15.114881, 8.746962, 3.941538, -4.062244, 13.539957, 3.941525, -15.114881, 8.746962, 0.000002, -0.120727, 18.679285, 0.000007, -17.594904, 10.178804, 3.941525, -15.114881, 8.746962, 2.435999, -11.102124, 6.430199, 2.435998, -2.556722, 9.775650, 2.435998, -2.556722, 9.775650, 3.941538, -4.062244, 13.539957, 3.941525, -15.114881, 8.746962, 0.000007, -17.594904, 10.178804, 0.000002, -0.120727, 18.679285, -3.941512, -4.062244, 15.128210, -3.941512, -4.062244, 15.128210, -3.941512, -15.114881, 8.746962, 0.000007, -17.594904, 10.178804, 3.941536, 3.820774, 15.128210, 0.000002, -0.120727, 18.679285, 3.941536, 3.820774, 15.128210, 2.435998, 2.315267, 9.775652, 3.941536, 3.820774, 15.128210, 2.435998, 2.315267, 9.775652, -3.941512, 3.820807, 13.539961, -2.435989, 2.315266, 9.775652, -3.941512, 3.820807, 13.539961, 0.000002, -0.120727, 18.679285, 0.000002, -0.120727, 18.679285, -3.941512, 3.820807, 13.539961, 0.000007, 17.353449, 10.178814, 3.941525, 14.873430, 8.746973, 0.000007, 17.353449, 10.178814, 3.941525, 14.873430, 8.746973, 2.435999, 10.860673, 6.430202, 3.941525, 14.873430, 8.746973, 2.435999, 10.860673, 6.430202, -2.435986, 10.860673, 6.430202, 2.435999, 10.860677, -6.250025, 2.435999, 10.860673, 6.430202, -2.435986, 10.860673, 6.430202, -3.941512, 14.873430, 8.746973, -2.435986, 10.860673, 6.430202, -3.941512, 14.873430, 8.746973, 0.000007, 17.353449, 10.178814, -3.941512, 14.873430, 8.746973, 0.000007, 17.353456, -9.998627, 3.941525, 14.873433, -8.566788, 3.941539, 3.820780, -14.948035, 3.941539, 3.820780, -14.948035, 0.000002, -0.120718, -18.822365, 0.000007, 17.353456, -9.998627, 2.435999, 10.860677, -6.250025, -0.000002, -0.120719, -11.325150, 2.435998, 2.315271, -9.918728, -2.435986, 10.860677, -6.250025, -2.435989, 2.315270, -9.918731, -0.000002, -0.120719, -11.325150, 2.435999, 10.860677, -6.250025, -2.435986, 10.860677, -6.250025, -0.000002, -0.120719, -11.325150, -2.435986, 10.860677, -6.250025, -3.941512, 14.873433, -8.566788, -3.941512, 3.820808, -13.683040, -3.941512, 3.820808, -13.683040, -2.435989, 2.315270, -9.918731, -2.435986, 10.860677, -6.250025, -3.941512, 14.873433, -8.566788, 0.000007, 17.353456, -9.998627, 0.000002, -0.120718, -18.822365, 0.000002, -0.120718, -18.822365, -3.941512, 3.820808, -13.683040, -3.941512, 14.873433, -8.566788, 3.941525, 14.873433, -8.566788, 2.435999, 10.860677, -6.250025, 2.435998, 2.315271, -9.918728, 2.435998, 2.315271, -9.918728, 3.941539, 3.820780, -14.948035, 3.941525, 14.873433, -8.566788, -0.000002, -0.120726, 11.182074, -2.435986, -11.102124, 6.430199, -2.435991, -2.556715, 9.775656, 2.435999, -11.102124, 6.430199, -2.435986, -11.102124, 6.430199, -0.000002, -0.120726, 11.182074, 2.435998, -2.556722, 9.775650, 2.435999, -11.102124, 6.430199, -0.000002, -0.120726, 11.182074, -0.000002, -0.120726, 11.182074, 2.435999, 10.860673, 6.430202, 2.435998, 2.315267, 9.775652, -0.000002, -0.120726, 11.182074, 2.435999, 10.860673, 6.430202, -2.435989, 2.315266, 9.775652, -0.000002, -0.120726, 11.182074, -2.435991, -2.556715, 9.775656, -3.941512, -15.114881, 8.746962, -3.941512, -4.062244, 15.128210, -3.941512, -15.114881, 8.746962, -2.435991, -2.556715, 9.775656, -2.435986, -11.102124, 6.430199
    };    
    
    GLfloat textureCoords[ ] = { 
        0.462371, 0.457327, 0.000000, 0.366348, 0.421955, 0.000000, 0.274115, 0.367637, 0.000000, 0.642259, 0.364723, 0.000000, 0.558393, 0.421955, 0.000000, 0.460785, 0.728649, 0.000000, 0.366168, 0.697142, 0.000000, 0.277770, 0.646163, 0.000000, 0.721535, 0.367787, 0.000000, 0.555401, 0.697142, 0.000000, 0.451678, 0.551533, 0.000000, 0.548022, 0.588319, 0.000000, 0.630829, 0.647839, 0.000000, 0.716518, 0.647839, 0.000000, 0.355334, 0.588318, 0.000000, 0.456685, 0.731713, 0.000000, 0.551301, 0.700206, 0.000000, 0.639563, 0.649227, 0.000000, 0.717798, 0.649227, 0.000000, 0.362069, 0.700206, 0.000000, 0.567599, 0.145913, 0.000000, 0.647879, 0.154255, 0.000000, 0.730534, 0.141227, 0.000000, 0.684366, 0.090544, 0.000000, 0.376789, 0.132885, 0.000000, 0.557839, 0.863346, 0.000000, 0.637340, 0.856896, 0.000000, 0.673674, 0.920992, 0.000000, 0.710007, 0.856896, 0.000000, 0.372285, 0.879135, 0.000000, 0.372285, 0.879135, 0.000000, 0.266016, 0.856896, 0.000000, 0.637340, 0.856897, 0.000000, 0.557839, 0.863346, 0.000000, 0.378781, 0.132886, 0.000000, 0.284839, 0.163736, 0.000000, 0.645887, 0.154254, 0.000000, 0.565608, 0.145912, 0.000000, 0.462370, 0.013336, 0.000000, 0.460379, 0.457327, 0.000000, 0.556402, 0.421955, 0.000000, 0.640267, 0.364723, 0.000000, 0.724482, 0.364723, 0.000000, 0.359819, 0.408567, 0.000000, 0.456604, 0.730640, 0.000000, 0.361988, 0.699134, 0.000000, 0.273726, 0.648155, 0.000000, 0.639482, 0.648155, 0.000000, 0.551220, 0.699134, 0.000000, 0.451678, 0.995523, 0.000000, 0.455661, 0.551533, 0.000000, 0.359317, 0.588318, 0.000000, 0.276511, 0.647839, 0.000000, 0.634812, 0.647839, 0.000000, 0.552006, 0.588318, 0.000000, 0.458596, 0.730640, 0.000000, 0.363980, 0.699134, 0.000000, 0.275718, 0.648155, 0.000000, 0.641474, 0.648155, 0.000000, 0.553212, 0.699134, 0.000000, 0.362646, 0.412793, 0.000000, 0.379825, 0.135690, 0.000000, 0.379825, 0.135690, 0.000000, 0.726474, 0.364723, 0.000000, 0.726474, 0.364723, 0.000000, 0.720854, 0.154255, 0.000000, 0.460785, 0.284658, 0.000000, 0.366168, 0.316165, 0.000000, 0.460785, 0.284658, 0.000000, 0.366031, 0.316165, 0.000000, 0.277770, 0.367144, 0.000000, 0.366031, 0.697142, 0.000000, 0.366031, 0.316165, 0.000000, 0.641476, 0.651166, 0.000000, 0.721535, 0.651166, 0.000000, 0.641476, 0.367787, 0.000000, 0.641476, 0.651166, 0.000000, 0.643663, 0.367144, 0.000000, 0.555401, 0.316165, 0.000000, 0.643663, 0.646163, 0.000000, 0.643663, 0.367144, 0.000000, 0.555401, 0.316165, 0.000000, 0.460785, 0.284658, 0.000000, 0.555401, 0.316165, 0.000000, 0.451678, 0.551533, 0.000000, 0.355334, 0.588319, 0.000000, 0.451678, 0.551533, 0.000000, 0.548022, 0.588319, 0.000000, 0.548022, 0.588319, 0.000000, 0.630829, 0.647839, 0.000000, 0.548022, 0.588319, 0.000000, 0.451678, 0.551533, 0.000000, 0.548022, 0.588319, 0.000000, 0.451678, 0.995523, 0.000000, 0.451678, 0.995523, 0.000000, 0.266016, 0.856896, 0.000000, 0.272528, 0.647839, 0.000000, 0.266016, 0.856896, 0.000000, 0.451678, 0.995523, 0.000000, 0.456685, 0.287722, 0.000000, 0.551301, 0.319229, 0.000000, 0.456685, 0.287722, 0.000000, 0.551301, 0.319229, 0.000000, 0.639563, 0.370208, 0.000000, 0.551301, 0.319229, 0.000000, 0.639563, 0.370208, 0.000000, 0.717798, 0.370207, 0.000000, 0.639563, 0.370208, 0.000000, 0.273807, 0.370207, 0.000000, 0.362069, 0.319229, 0.000000, 0.273807, 0.649227, 0.000000, 0.273807, 0.370207, 0.000000, 0.362069, 0.319229, 0.000000, 0.456685, 0.287722, 0.000000, 0.362069, 0.319229, 0.000000, 0.460379, 0.013336, 0.000000, 0.460379, 0.457326, 0.000000, 0.556402, 0.421955, 0.000000, 0.556402, 0.421955, 0.000000, 0.460379, 0.013336, 0.000000, 0.556402, 0.421955, 0.000000, 0.640267, 0.364723, 0.000000, 0.556402, 0.421955, 0.000000, 0.640267, 0.364723, 0.000000, 0.724482, 0.364723, 0.000000, 0.640267, 0.364723, 0.000000, 0.682375, 0.090544, 0.000000, 0.460379, 0.457326, 0.000000, 0.460379, 0.013336, 0.000000, 0.364357, 0.421955, 0.000000, 0.460379, 0.457326, 0.000000, 0.269752, 0.362104, 0.000000, 0.363626, 0.411397, 0.000000, 0.375423, 0.134329, 0.000000, 0.375423, 0.134329, 0.000000, 0.281867, 0.165683, 0.000000, 0.269752, 0.362104, 0.000000, 0.355334, 0.588319, 0.000000, 0.272528, 0.647839, 0.000000, 0.355334, 0.588319, 0.000000, 0.673674, 0.920992, 0.000000, 0.716518, 0.647839, 0.000000, 0.630829, 0.647839, 0.000000, 0.673674, 0.920992, 0.000000, 0.630829, 0.647839, 0.000000, 0.710007, 0.856896, 0.000000, 0.716518, 0.647839, 0.000000, 0.673674, 0.920992, 0.000000, 0.460379, 0.013336, 0.000000, 0.565608, 0.145913, 0.000000, 0.460379, 0.013336, 0.000000, 0.645887, 0.154254, 0.000000, 0.565608, 0.145913, 0.000000, 0.645887, 0.154254, 0.000000, 0.645887, 0.154254, 0.000000, 0.730534, 0.141227, 0.000000, 0.730534, 0.141227, 0.000000, 0.645887, 0.154254, 0.000000, 0.682375, 0.090544, 0.000000, 0.730534, 0.141227, 0.000000, 0.378220, 0.132117, 0.000000, 0.275727, 0.366249, 0.000000, 0.275727, 0.366249, 0.000000, 0.284666, 0.163471, 0.000000, 0.378220, 0.132117, 0.000000, 0.460379, 0.013336, 0.000000, 0.376789, 0.132886, 0.000000, 0.364357, 0.421955, 0.000000, 0.376789, 0.132886, 0.000000, 0.456604, 0.286650, 0.000000, 0.361988, 0.318156, 0.000000, 0.456604, 0.286650, 0.000000, 0.361988, 0.318156, 0.000000, 0.273726, 0.369135, 0.000000, 0.361988, 0.318156, 0.000000, 0.717717, 0.369135, 0.000000, 0.639482, 0.369135, 0.000000, 0.717717, 0.648155, 0.000000, 0.717717, 0.369135, 0.000000, 0.639482, 0.369135, 0.000000, 0.551220, 0.318156, 0.000000, 0.639482, 0.369135, 0.000000, 0.551220, 0.318156, 0.000000, 0.456604, 0.286650, 0.000000, 0.551220, 0.318156, 0.000000, 0.451678, 0.995524, 0.000000, 0.548023, 0.588319, 0.000000, 0.557839, 0.863346, 0.000000, 0.548023, 0.588319, 0.000000, 0.451678, 0.995524, 0.000000, 0.451678, 0.551533, 0.000000, 0.548023, 0.588319, 0.000000, 0.630829, 0.647839, 0.000000, 0.637340, 0.856897, 0.000000, 0.637340, 0.856897, 0.000000, 0.557839, 0.863346, 0.000000, 0.548023, 0.588319, 0.000000, 0.451678, 0.551533, 0.000000, 0.451678, 0.995524, 0.000000, 0.372284, 0.879135, 0.000000, 0.372284, 0.879135, 0.000000, 0.355334, 0.588319, 0.000000, 0.451678, 0.551533, 0.000000, 0.376268, 0.879135, 0.000000, 0.455662, 0.995523, 0.000000, 0.376268, 0.879135, 0.000000, 0.269999, 0.856896, 0.000000, 0.376268, 0.879135, 0.000000, 0.269999, 0.856896, 0.000000, 0.561822, 0.863345, 0.000000, 0.641324, 0.856896, 0.000000, 0.561822, 0.863345, 0.000000, 0.455662, 0.995523, 0.000000, 0.455662, 0.995523, 0.000000, 0.561822, 0.863345, 0.000000, 0.458596, 0.286650, 0.000000, 0.363980, 0.318156, 0.000000, 0.458596, 0.286650, 0.000000, 0.363980, 0.318156, 0.000000, 0.275718, 0.369135, 0.000000, 0.363980, 0.318156, 0.000000, 0.719709, 0.369135, 0.000000, 0.641474, 0.369135, 0.000000, 0.719709, 0.648155, 0.000000, 0.719709, 0.369135, 0.000000, 0.641474, 0.369135, 0.000000, 0.553212, 0.318156, 0.000000, 0.641474, 0.369135, 0.000000, 0.553212, 0.318156, 0.000000, 0.458596, 0.286650, 0.000000, 0.553212, 0.318156, 0.000000, 0.462371, 0.461310, 0.000000, 0.366348, 0.425939, 0.000000, 0.378780, 0.136869, 0.000000, 0.378780, 0.136869, 0.000000, 0.462371, 0.017320, 0.000000, 0.462371, 0.461310, 0.000000, 0.726473, 0.368706, 0.000000, 0.684366, 0.094527, 0.000000, 0.720853, 0.158238, 0.000000, 0.642259, 0.368706, 0.000000, 0.647879, 0.158238, 0.000000, 0.684366, 0.094527, 0.000000, 0.726473, 0.368706, 0.000000, 0.642259, 0.368706, 0.000000, 0.684366, 0.094527, 0.000000, 0.642259, 0.368706, 0.000000, 0.558393, 0.425939, 0.000000, 0.567599, 0.149897, 0.000000, 0.567599, 0.149897, 0.000000, 0.647879, 0.158238, 0.000000, 0.642259, 0.368706, 0.000000, 0.558393, 0.425939, 0.000000, 0.462371, 0.461310, 0.000000, 0.462371, 0.017320, 0.000000, 0.462371, 0.017320, 0.000000, 0.567599, 0.149897, 0.000000, 0.558393, 0.425939, 0.000000, 0.361265, 0.409500, 0.000000, 0.274145, 0.368182, 0.000000, 0.284457, 0.166546, 0.000000, 0.284457, 0.166546, 0.000000, 0.377775, 0.132774, 0.000000, 0.361265, 0.409500, 0.000000, 0.673674, 0.920992, 0.000000, 0.716518, 0.647839, 0.000000, 0.710006, 0.856897, 0.000000, 0.630829, 0.647839, 0.000000, 0.716518, 0.647839, 0.000000, 0.673674, 0.920992, 0.000000, 0.637340, 0.856897, 0.000000, 0.630829, 0.647839, 0.000000, 0.673674, 0.920992, 0.000000, 0.677656, 0.920992, 0.000000, 0.720501, 0.647839, 0.000000, 0.713989, 0.856896, 0.000000, 0.677656, 0.920992, 0.000000, 0.720501, 0.647839, 0.000000, 0.641324, 0.856896, 0.000000, 0.677656, 0.920992, 0.000000, 0.266016, 0.856897, 0.000000, 0.355334, 0.588319, 0.000000, 0.372284, 0.879135, 0.000000, 0.355334, 0.588319, 0.000000, 0.266016, 0.856897, 0.000000, 0.272527, 0.647839, 0.000000
    };
    
    GLfloat normals[ ] = {
        0.469522, -0.010598, -0.882857, 0.423198, 0.532556, -0.732999, -0.160498, 0.942532, 0.293041, -0.317732, -0.645482, 0.694549, 0.246977, -0.839404, -0.484153, 1.000000, 0.000000, -0.000000, 0.846395, 0.532556, 0.000000, -0.351275, 0.936272, 0.000000, -1.000000, 0.000000, 0.000000, 0.515448, -0.856921, -0.000000, -0.461376, 0.013373, 0.887104, -0.247252, 0.833788, 0.493623, 0.295019, 0.637868, -0.711399, 0.392898, 0.022129, -0.919316, -0.273205, -0.853979, 0.442808, -1.000000, 0.000000, -0.000000, -0.515447, 0.856921, 0.000000, 0.764805, 0.644262, 0.000000, 1.000000, 0.000000, 0.000000, -0.515447, -0.856921, -0.000000, 0.185114, -0.901219, -0.391839, -0.366830, -0.367906, 0.854448, 0.377222, -0.189965, 0.906431, -0.401482, 0.000000, 0.915867, -0.416324, -0.410956, -0.811042, 0.165270, -0.909638, 0.381111, -0.347927, -0.336826, -0.874925, 0.373737, -0.000001, -0.927535, 0.359210, 0.170533, -0.917544, -0.286934, -0.750937, 0.594779, 0.286934, 0.750936, 0.594780, -0.135752, 0.956878, -0.256819, 0.347927, 0.336825, -0.874926, -0.165270, 0.909638, 0.381110, 0.416324, 0.410956, -0.811042, -0.109412, 0.960770, 0.254851, 0.358326, 0.301326, 0.883632, -0.182729, 0.900436, -0.394747, 0.439141, -0.108839, -0.891801, 0.010598, -0.469522, -0.882857, 0.838901, -0.249891, -0.483529, 0.624418, 0.347091, 0.699736, 0.000000, 0.500000, 0.866026, -0.936272, 0.175637, 0.304213, 0.000000, -1.000000, -0.000000, 0.856921, -0.515447, -0.000000, 0.936272, 0.351275, 0.000000, -0.644262, 0.764805, 0.000000, -0.856921, -0.515447, -0.000000, 0.421933, -0.138838, 0.895934, -0.013374, 0.461376, 0.887104, 0.853979, 0.273205, 0.442808, 0.923338, -0.139983, -0.357563, -0.645254, -0.299875, -0.702654, -0.834433, 0.243565, 0.494367, 0.000000, 1.000000, 0.000000, 0.856921, 0.515447, 0.000000, 0.936272, -0.351275, -0.000000, -0.644262, -0.764805, -0.000000, -0.856921, 0.515447, 0.000000, -0.175639, 0.936272, 0.304212, -0.175639, 0.936272, 0.304212, -0.109412, 0.960770, 0.254851, -0.419521, -0.000000, 0.907746, -0.390900, -0.134783, 0.910511, -0.390900, -0.134783, 0.910511, 0.846395, 0.532556, 0.000000, 0.846395, 0.532556, 0.000000, 0.846395, 0.532555, 0.000000, -0.351275, 0.936272, 0.000000, -0.351275, 0.936272, 0.000000, -0.351275, 0.936272, 0.000000, -0.351275, 0.936272, 0.000000, -1.000000, 0.000000, 0.000000, -1.000000, 0.000000, 0.000000, -1.000000, 0.000000, 0.000000, -1.000000, 0.000000, 0.000000, -0.351275, -0.936272, -0.000000, -0.351275, -0.936272, -0.000000, -0.351275, -0.936272, -0.000000, -0.351275, -0.936272, -0.000000, 0.846395, -0.532556, -0.000000, 0.846395, -0.532556, -0.000000, 0.846395, -0.532555, -0.000000, 0.423197, 0.532555, 0.733000, 0.423197, 0.532555, 0.733000, 0.409900, 0.349250, 0.842619, -0.138551, -0.937403, -0.319498, -0.139983, -0.923338, -0.357564, -0.139983, -0.923338, -0.357564, 0.374790, -0.515701, 0.770445, 0.374790, -0.515701, 0.770445, 0.310914, -0.623933, 0.716966, -0.374790, 0.515700, 0.770445, -0.310914, 0.623932, 0.716966, 0.139983, -0.923338, -0.357563, 0.139983, -0.923338, -0.357563, 0.134552, -0.963111, -0.233051, -0.409900, -0.349250, 0.842619, -0.846395, 0.532556, 0.000000, -0.846395, 0.532556, 0.000000, -0.846395, 0.532556, 0.000000, 0.351275, 0.936272, 0.000000, 0.351275, 0.936272, 0.000000, 0.351275, 0.936272, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 0.351275, -0.936272, -0.000000, 0.351275, -0.936272, -0.000000, 0.351275, -0.936272, -0.000000, 0.351275, -0.936272, -0.000000, -0.846395, -0.532556, -0.000000, -0.846395, -0.532556, -0.000000, -0.846395, -0.532556, -0.000000, -0.385169, 0.519444, -0.762773, -0.385169, 0.519444, -0.762773, -0.385169, 0.519444, -0.762773, -0.333934, 0.606691, -0.721397, -0.333934, 0.606691, -0.721397, 0.148275, 0.926678, 0.345373, 0.148275, 0.926678, 0.345373, 0.146418, 0.937289, 0.316310, 0.500000, -0.000000, 0.866025, 0.500000, -0.000000, 0.866025, 0.383729, -0.232074, 0.893809, 0.293925, -0.240212, 0.925152, -0.414589, -0.392451, -0.821035, -0.414589, -0.392451, -0.821035, -0.423198, -0.532556, -0.733000, -0.423197, -0.532556, -0.733000, 0.175638, -0.936272, 0.304213, 0.175638, -0.936272, 0.304213, 0.175638, -0.936272, 0.304213, 0.175638, -0.936272, 0.304214, 0.175638, -0.936272, 0.304214, 0.175639, -0.936272, 0.304214, -0.139983, 0.923338, -0.357563, -0.139983, 0.923338, -0.357563, -0.134552, 0.963112, -0.233047, -0.397134, 0.000000, -0.917761, -0.397134, 0.000000, -0.917761, -0.397134, 0.000000, -0.917761, -0.359211, 0.170533, -0.917544, -0.359211, 0.170533, -0.917544, -0.359211, -0.170533, -0.917544, -0.359211, -0.170533, -0.917544, -0.359211, -0.170533, -0.917544, 0.606691, -0.333934, -0.721398, 0.606691, -0.333934, -0.721398, 0.519444, -0.385168, -0.762774, 0.926678, 0.148275, 0.345373, 0.937289, 0.146418, 0.316310, 0.937289, 0.146418, 0.316310, -0.232074, 0.383728, 0.893809, -0.232074, 0.383728, 0.893809, 0.000000, 0.500000, 0.866026, -0.240212, 0.293925, 0.925152, -0.240212, 0.293925, 0.925152, -0.240212, 0.293925, 0.925152, -0.936272, 0.175637, 0.304213, -0.936272, 0.175637, 0.304213, -0.936273, 0.175636, 0.304213, -0.936273, 0.175637, 0.304213, -0.936273, 0.175637, 0.304213, -0.392451, -0.414588, -0.821035, -0.392451, -0.414588, -0.821035, -0.532556, -0.423197, -0.733000, -0.532556, -0.423197, -0.733000, 0.532555, -0.846395, -0.000000, 0.532555, -0.846395, -0.000000, 0.532556, -0.846395, -0.000000, 0.936272, 0.351275, 0.000000, 0.936272, 0.351275, 0.000000, 0.936272, 0.351275, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, -0.936272, 0.351275, 0.000000, -0.936272, 0.351275, 0.000000, -0.936272, 0.351275, 0.000000, -0.532555, -0.846395, -0.000000, -0.532555, -0.846395, -0.000000, -0.532556, -0.846395, -0.000000, 0.623932, -0.310914, 0.716966, 0.623932, -0.310914, 0.716966, 0.623932, -0.310914, 0.716966, 0.515701, -0.374790, 0.770445, 0.515701, -0.374790, 0.770445, 0.515701, -0.374790, 0.770445, 0.923338, 0.139983, -0.357564, 0.923338, 0.139983, -0.357564, 0.923338, 0.139983, -0.357564, 0.937402, 0.138552, -0.319502, 0.937402, 0.138552, -0.319502, 0.937402, 0.138552, -0.319502, -0.349250, -0.409900, 0.842619, -0.349250, -0.409900, 0.842619, -0.349250, -0.409900, 0.842619, -0.532556, -0.423198, 0.732999, -0.532556, -0.423198, 0.732999, -0.532556, -0.423198, 0.732999, 0.532555, 0.423197, 0.733000, 0.349251, 0.409899, 0.842619, 0.349251, 0.409899, 0.842619, 0.963111, -0.134552, -0.233053, 0.963111, -0.134552, -0.233053, 0.923338, -0.139983, -0.357563, -0.927033, -0.149186, -0.344024, -0.937447, -0.126910, -0.324171, -0.937447, -0.126910, -0.324171, -0.515700, 0.374790, 0.770445, -0.623934, 0.310913, 0.716966, -0.623934, 0.310913, 0.716966, 0.532555, 0.846395, 0.000000, 0.532555, 0.846395, 0.000000, 0.532556, 0.846395, 0.000000, 0.936272, -0.351275, -0.000000, 0.936272, -0.351275, -0.000000, 0.936272, -0.351275, -0.000000, 0.000000, -1.000000, -0.000000, 0.000000, -1.000000, -0.000000, 0.000000, -1.000000, -0.000000, 0.000000, -1.000000, -0.000000, -0.936272, -0.351275, -0.000000, -0.936272, -0.351275, -0.000000, -0.936272, -0.351275, -0.000000, -0.532555, 0.846395, 0.000000, -0.532555, 0.846395, 0.000000, -0.532556, 0.846395, 0.000000, 0.532556, 0.423198, -0.732999, 0.532556, 0.423198, -0.732999, 0.532556, 0.423198, -0.732999, 0.392451, 0.414589, -0.821035, 0.392451, 0.414589, -0.821035, 0.392451, 0.414589, -0.821035, -0.134785, -0.390900, 0.910511, -0.134785, -0.390900, 0.910511, -0.134785, -0.390900, 0.910511, 0.134784, -0.390900, 0.910511, 0.134784, -0.390900, 0.910511, 0.134784, -0.390900, 0.910511, 0.000000, -0.419521, 0.907746, 0.000000, -0.419521, 0.907746, 0.000000, -0.419521, 0.907746, -0.929296, -0.155149, 0.335168, -0.929296, -0.155149, 0.335168, -0.929296, -0.155149, 0.335168, -0.937414, -0.137371, 0.319974, -0.937414, -0.137371, 0.319974, -0.937414, -0.137371, 0.319974, -0.519444, 0.385169, -0.762773, -0.519444, 0.385169, -0.762773, -0.519444, 0.385169, -0.762773, -0.606691, 0.333934, -0.721397, -0.606691, 0.333934, -0.721397, -0.606691, 0.333934, -0.721397, 0.926678, -0.148275, 0.345373, 0.926678, -0.148275, 0.345373, 0.926678, -0.148275, 0.345373, 0.959207, -0.141351, 0.244829, 0.959207, -0.141351, 0.244829, 0.959207, -0.141351, 0.244829, 0.170533, 0.359211, -0.917544, 0.170533, 0.359211, -0.917544, 0.170533, 0.359211, -0.917544, 0.000000, 0.397134, -0.917761, 0.000000, 0.397134, -0.917761, 0.000000, 0.397134, -0.917761, -0.170534, 0.359210, -0.917544, -0.170534, 0.359210, -0.917544, -0.170533, 0.359210, -0.917544, -0.170534, -0.359210, -0.917544, -0.170534, -0.359210, -0.917544, -0.170534, -0.359210, -0.917544, 0.000000, -0.397133, -0.917761, 0.000000, -0.397133, -0.917761, 0.170535, -0.359210, -0.917543, 0.170535, -0.359210, -0.917543, -0.963111, 0.134552, -0.233050, -0.963111, 0.134552, -0.233050, -0.963111, 0.134552, -0.233050, -0.923338, 0.139983, -0.357564, -0.923338, 0.139983, -0.357564, -0.923338, 0.139983, -0.357564
    };
    
    GLushort faces[ ] = { 
        34, 1, 0, 0, 38, 34, 2, 60, 61, 62, 35, 2, 23, 3, 63, 23, 64, 65, 21, 3, 23, 20, 4, 3, 3, 21, 20, 4, 38, 0, 38, 4, 20, 66, 67, 6, 6, 5, 68, 69, 70, 7, 7, 71, 72, 73, 74, 8, 8, 75, 76, 77, 78, 9, 9, 79, 80, 81, 82, 5, 5, 9, 83, 84, 85, 30, 30, 49, 86, 26, 87, 25, 88, 26, 89, 90, 91, 49, 49, 25, 92, 93, 11, 10, 11, 94, 33, 32, 12, 11, 11, 33, 32, 27, 13, 12, 27, 28, 13, 32, 27, 12, 95, 14, 96, 14, 97, 29, 98, 10, 29, 14, 29, 10, 99, 100, 16, 16, 15, 101, 102, 103, 17, 17, 16, 104, 105, 106, 18, 18, 17, 107, 108, 109, 19, 19, 110, 111, 112, 113, 15, 15, 19, 114, 115, 116, 117, 118, 37, 119, 120, 121, 36, 36, 37, 122, 123, 124, 22, 36, 125, 22, 126, 36, 22, 127, 128, 24, 24, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 31, 31, 30, 139, 140, 141, 142, 143, 144, 26, 145, 146, 147, 148, 149, 40, 40, 39, 150, 151, 41, 40, 40, 152, 153, 154, 155, 41, 41, 156, 42, 157, 158, 159, 160, 43, 161, 162, 163, 164, 165, 39, 166, 167, 168, 39, 169, 170, 45, 45, 44, 171, 172, 173, 46, 46, 45, 174, 175, 176, 47, 47, 177, 178, 179, 180, 48, 48, 47, 181, 182, 183, 44, 44, 48, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 51, 50, 50, 204, 205, 206, 51, 207, 51, 208, 52, 209, 54, 53, 53, 210, 211, 54, 212, 50, 213, 54, 214, 215, 216, 56, 56, 55, 217, 218, 219, 57, 57, 56, 220, 221, 222, 58, 58, 223, 224, 225, 226, 59, 59, 58, 227, 228, 229, 55, 55, 59, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 53, 277, 278, 53, 279, 280, 281, 282, 283, 284, 285
    };
    
    //int verticesLength = sizeof(vertices) / sizeof(GLfloat);
    int facesLength = sizeof(faces) / sizeof(GLushort);
    
    glEnable(GL_TEXTURE_2D); 
    glEnableClientState(GL_NORMAL_ARRAY);          
    glEnable(GL_NORMALIZE);     
    
    glEnable(GL_CULL_FACE); 
    glCullFace(GL_BACK);    
    
    glVertexPointer(3, GL_FLOAT, 0, vertices);
    glBindTexture(GL_TEXTURE_2D, textureID);     
    glTexCoordPointer(3, GL_FLOAT, 0, textureCoords);        
    glNormalPointer(GL_FLOAT, 0, normals);
    
    glColor4f(1, 1, 1, 1); 
    
    glPushMatrix();  
    glTranslatef(0, 0, -100);
    static int angle = 0; angle += 2;
    glRotatef(angle, 1, 1, 1);    
    //glDrawArrays(GL_TRIANGLE_STRIP, 0, verticesLength/3); 
    glDrawElements(GL_TRIANGLES, facesLength, GL_UNSIGNED_SHORT, faces);        
    glPopMatrix();
    
    //Cleanup
    glDisable(GL_TEXTURE_2D);   
    glDisable(GL_NORMALIZE);     
    glDisable(GL_CULL_FACE);    
}

- (Tex *) getTex: (NSString*) name isImage: (bool) imgFlag { 
	Tex *tex = [[self getDictionary] objectForKey: name];
	if (!tex) {
		tex = [[Tex alloc] init];
        if (imgFlag) {            
            [tex createTexFromImage: name];             
        } else {
            [tex createTexFromString: name];
        }    
        [[self getDictionary] setObject: tex forKey: name];
        [tex release];         
	}          
	return tex;  
}

//Camera algorithm based on mesa3d.org (Open-Source Mesa 3D Graphics Library)
- (void) setCameraX: (GLfloat) camX 
            cameraY: (GLfloat) camY 
            cameraZ: (GLfloat) camZ
            lookAtX: (GLfloat) atX 
            lookAtY: (GLfloat) atY 
            lookAtZ: (GLfloat) atZ {
    
    glLoadIdentity();
    
    GLfloat matrix[16];
    
    GLfloat forward[3]; 
    GLfloat upward[3]; 
    GLfloat sideward[3];        
    
    upward[0] = 0; //x
    upward[1] = 1; //y
    upward[2] = 0; //z
    //[self normalizeVector: upward]; 
    
    forward[0] = camX - atX;
    forward[1] = camY - atY;
    forward[2] = camZ - atZ;    
    [self normalizeVector: forward];
    
    sideward[0] =  upward[1] * forward[2] - upward[2] * forward[1];
    sideward[1] = -upward[0] * forward[2] + upward[2] * forward[0];
    sideward[2] =  upward[0] * forward[1] - upward[1] * forward[0];
    
    upward[0] =  forward[1] * sideward[2] - forward[2] * sideward[1];
    upward[1] = -forward[0] * sideward[2] + forward[2] * sideward[0];
    upward[2] =  forward[0] * sideward[1] - forward[1] * sideward[0];
    
    [self normalizeVector: sideward];
    [self normalizeVector: forward];

    matrix[0] = sideward[0];
    matrix[1] = sideward[1];
    matrix[2] = sideward[2];
    matrix[3] = 0;
    
    matrix[4] = upward[0];
    matrix[5] = upward[1];
    matrix[6] = upward[2];
    matrix[7] = 0;
    
    matrix[8] = forward[0];
    matrix[9] = forward[1];
    matrix[10] = forward[2];    
    matrix[11] = 0;
    
    matrix[12] = 0;
    matrix[13] = 0;
    matrix[14] = 0;
    matrix[15] = 1;
    
    glMultMatrixf(matrix);
    
    glTranslatef(-camX, -camY, -camZ);    
}

- (void) normalizeVector: (GLfloat [3]) v {
    float length = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
    
    if (length != 0) { 
        v[0] /= length;
        v[1] /= length;
        v[2] /= length;
    }
}

#pragma mark ============================= Helper Methods ===============================

- (NSMutableDictionary *) getDictionary {
	if (!dictionary) { 
		dictionary = [[NSMutableDictionary alloc] init]; 
	}
	return dictionary;
}

- (void) removeFromDictionary: (NSString*) name {
    [[self getDictionary] removeObjectForKey: name];
} 

- (int) getRndBetween: (int) bottom and: (int) top {		
	int rnd = bottom + (arc4random() % (top+1-bottom)); 
	return rnd;
} 

- (void) dealloc {      
    [dictionary release];
    [super dealloc];
}

@end
